package com.hy.utils.download;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 根据网址下载图片
 */
@Component
public class HeardDownload {

    public static void HdDownload(String headurl,String headname){
        //根据地址下载图片
       // String headname= SaltUtils.getSalt(6);
        //"E:"+File.separator+"test/"+headname+".jpg";
        String path = headname;
       // String urlList = "https://tfs.alipayobjects.com/images/partner/TB1kxt1Xhem.eJkUQclXXbG0pXa";
        URL url = null;
        try {
            url = new URL(headurl);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            System.out.println("头像保存成功");
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
